﻿using InChatter.Client.Service;
using InChatter.Client.Service.ChatServer;
using InChatter.Client.WPFClient.Utility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InChatter.Client.WPFClient.ViewModel
{
    public class MessagePoolWindowViewModel
    {
                #region 属性变量
        /// <summary>
        /// 委托新消息弹出窗口
        /// </summary>
        internal Action<bool> MessageShowOrClose { get; set; }

        private ObservableCollection<InChatterMessage> m_Messages = new ObservableCollection<InChatterMessage>();
        /// <summary>
        /// 当前消息
        /// </summary>
        public ObservableCollection<InChatterMessage> Messages
        {
            get
            {
                return m_Messages;
            }
            set
            {
                m_Messages = value;
                this.OnPropertyChanged("Messages");
            }
        }
        #endregion

        /// <summary>
        /// 关闭消息框
        /// </summary>
        public DelegateCommand ExcuteCloseMessage { get; private set; }
        /// <summary>
        /// 执行连接到消息
        /// </summary>
        public DelegateCommand<object> ExcuteLinkOrIgnoreMesssge { get; private set; }

        public MessagePoolWindowViewModel()
        {
            ChatMsgCenter.Instance.MessageReceived += new Action<InChatterMessage>(Instance_ReceivedMessageEvent);
            ChatMsgCenter.Instance.MessageRead += new Action<InChatterMessage>(Instance_MessageRead);
            ChatMsgCenter.Instance.TypeChanged += new Action<string, string>(Instance_TypeChanged);

            ExcuteCloseMessage = new DelegateCommand(() =>
            {
                MessageShowOrClose(false);
            });
            //执行忽略或已读消息
            ExcuteLinkOrIgnoreMesssge = new DelegateCommand<object>((obj) =>
            {
                //执行已读
                if (obj is InChatterMessage)
                {
                    ProcessMessge(obj as InChatterMessage);
                }
                //执行忽略
                else if (obj is IList<InChatterMessage>)
                {
                    IgnoreMessge(obj as IList<InChatterMessage>);
                }
            });
        }

        #region 消息数据处理
        /// <summary>
        /// 接受新消息
        /// </summary>
        /// <param name="msg"></param>
        private void Instance_ReceivedMessageEvent(InChatter.Client.Service.ChatServer.InChatterMessage msg)
        {
            bool isRead = ChatMsgCenter.Instance.OnMessageReadDirectly(msg);
            if (msg.Type == "logon" && msg.Type == "logoff")
            {
                return;
            }
            if (isRead)
            {
                DataHelper.ReadMsg(msg);
            }
            else
            {
                Messages.Insert(0, msg);
                MessageShowOrClose(true);
                ChatMsgCenter.Instance.OnMessageCountChanged(Messages.Count);
                ChatMsgCenter.Instance.OnMessagePoolReceivedNew(msg);
            }
        }
        /// <summary>
        /// 消息已读
        /// </summary>
        /// <param name="msg"></param>
        private void Instance_MessageRead(InChatter.Client.Service.ChatServer.InChatterMessage msg)
        {
            ChatMsgCenter.Instance.OnMessageCountChanged(Messages.Count);
            Messages.Remove(msg);
            msg.IsRead = true;
            DataHelper.Update(msg);
        }

        private void Instance_TypeChanged(string type, string id)
        {
            if (type != "msg")
            {
                var result = this.Messages.Where(p => p.Type == type).ToList();
                IgnoreMessge(result);
            }
            else
            {
                var result = this.Messages.Where(p => p.Type == type && p.SenderID == id).ToList();
                IgnoreMessge(result);
            }
        }

        /// <summary>
        /// 用户忽略消息
        /// </summary>
        private void IgnoreMessge(IList<InChatter.Client.Service.ChatServer.InChatterMessage> Messages)
        {
            //remove from the list
            foreach (var message in Messages)
            {
                Instance_MessageRead(message);
                ChatMsgCenter.Instance.OnMessageIgnored(message);
            }
            MessageShowOrClose(false);
        }

        /// <summary>
        /// client click to read the message
        /// </summary>
        private void ProcessMessge(InChatter.Client.Service.ChatServer.InChatterMessage message)
        {
            MessageShowOrClose(false);

            if (message.Type == "msg")
            {
                var result = Messages.Where(p => p.SenderID == message.SenderID && message.Type == "msg").OrderBy(p => p.SendTime).ToList();
                ChatMsgCenter.Instance.OnRequestToRead(result);
                foreach (var item in result)
                {
                    Instance_MessageRead(item);
                }
            }
            else
            {
                var result = Messages.Where(p => p.Type == message.Type).OrderBy(p => p.SendTime).ToList();
                ChatMsgCenter.Instance.OnRequestToRead(result);
                foreach (var item in result)
                {
                    Instance_MessageRead(item);
                }
            }
        }
        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
